module OH_PAYLOAD_DSCRAM(
   GLB_RESET,
 // CPU config signals
   CFG_POS_PAYLOAD_SCRAM_EN,
   CFG_POS_PAYLOAD_SCRAM_MODE,
 // input data bus, the data/data enable before descram
   CLOCK155M,
   DBIN_DATA,
   DBIN_DATA_EN,

//   STOP_RX,
//   DBIN_J1,
//   DBIN_SPE,
//   DBIN_CNT8,
//
//   DBOUT_J1,
//   DBOUT_SPE,
//   DBOUT_CNT8,  
   
 // ouptut data bus, the data/data enable after descram
   DBOUT_DATA,
   DBOUT_DATA_EN
   );

input                      GLB_RESET;                      // reset, '1' is effective
input                      CFG_POS_PAYLOAD_SCRAM_EN;       // CPU configuration signal, '1' enable the POS payload scram function
input                      CFG_POS_PAYLOAD_SCRAM_MODE;
input                      CLOCK155M;                      // input data bus clock at 156MHz, also used as module work clock
input[63:0]                DBIN_DATA;                      //
input                      DBIN_DATA_EN;                   //
//input						STOP_RX;                       // STOP recieve.   1 stop
output reg[63:0]           DBOUT_DATA;                     //
output reg                 DBOUT_DATA_EN;                  //

//input	   					DBIN_J1;
//input	   					DBIN_SPE;
//input	[2:0]   			DBIN_CNT8;
//
//output						DBOUT_J1;
//output						DBOUT_SPE;
//output	[2:0]				DBOUT_CNT8;





reg[63:0]                  NSCRM_DATA;
reg                        NSCRM_DATA_EN;

reg[42:0]                  X43_REMAINDER;
reg[63:0]                  X43_DSCRM_DATA;
reg                        X43_DSCRM_DATA_EN;
wire[63:0]                 X43_Y;

reg                        DSEL_SCRAM_EN;
reg                        DSEL_SCRAM_MODE;


//reg 	   					DBIN_J1_d1;
//reg 	   					DBIN_SPE_d1;
//reg 	[2:0]   			DBIN_CNT8_d1;
//
//reg 						DBOUT_J1;
//reg 						DBOUT_SPE;
//reg 	[2:0]				DBOUT_CNT8;

//////////////////////////////////////////////////////////////////
///    cnt delay 2 cycle for rpoh
/////////////////////////////////////////////////////////////////
//always @( posedge GLB_RESET or posedge CLOCK155M ) begin
//	if ( GLB_RESET==1'b1 )
//	begin
//		DBIN_J1_d1               	<= 1'b0;
//		DBIN_SPE_d1    			<= 1'b0;
//		DBIN_CNT8_d1[2:0]			<= 3'h0;
//	end
//	else 
//	begin
//		DBIN_J1_d1               	<= DBIN_J1;
//		DBIN_SPE_d1    			<= DBIN_SPE;
//		DBIN_CNT8_d1[2:0]			<= DBIN_CNT8[2:0];
//	end
//end
//
//
//always @( posedge GLB_RESET or posedge CLOCK155M ) begin
//	if ( GLB_RESET==1'b1 )
//	begin
//		DBOUT_J1  				<=1'b0;
//		DBOUT_SPE 				<=1'b0;
//		DBOUT_CNT8[2:0]			<=3'h0;
//	end
//	else 
//	begin
//		DBOUT_J1  				<=DBIN_J1_d1;
//		DBOUT_SPE 				<=DBIN_SPE_d1;
//		DBOUT_CNT8[2:0]			<=DBIN_CNT8_d1[2:0];
//   end
//end


/////////////////////////////////////////////////////////////////







// X43+1 DESCRAM Polygram, use input 64bits data as Y[i] 0<=i<64; the remainder as R(i} 0<=i<43; the reusult after descram as X(i) 0<=i<64
// R[0]=X[0]           R[1]=X[1]            R[2]=X[2]            R[3]=X[3]           R[4]=X[4]
// R[5]=X[5]           R[6]=X[6]            R[7]=X[7]            R[8]=X[8]           R[9]=X[9]
// R[10]=X[10]         R[11]=X[11]          R[12]=X[12]          R[13]=X[13]         R[14]=X[14]
// R[15]=X[15]         R[16]=X[16]          R[17]=X[17]          R[18]=X[18]         R[19]=X[19]
// R[20]=X[20]         R[21]=X[21]          R[22]=X[22]          R[23]=X[23]         R[24]=X[24]
// R[25]=X[25]         R[26]=X[26]          R[27]=X[27]          R[28]=X[28]         R[29]=X[29]
// R[30]=X[30]         R[31]=X[31]          R[32]=X[32]          R[33]=X[33]         R[34]=X[34]
// R[35]=X[35]         R[36]=X[36]          R[37]=X[37]          R[38]=X[38]         R[39]=X[39]
// R[40]=X[40]         R[41]=X[41]          R[42]=X[42]
// X[0]=Y[0]+Y[43]     X[1]=Y[1]+Y[44]      X[2]=Y[2]+Y[45]      X[3]=Y[3]+Y[46]     X[4]=Y[4]+Y[47]
// X[5]=Y[5]+Y[48]     X[6]=Y[6]+Y[49]      X[7]=Y[7]+Y[50]      X[8]=Y[8]+Y[51]     X[9]=Y[9]+Y[52]
// X[10]=Y[10]+Y[53]   X[11]=Y[11]+Y[54]    X[12]=Y[12]+Y[55]    X[13]=Y[13]+Y[56]   X[14]=Y[14]+Y[57]
// X[15]=Y[15]+Y[58]   X[16]=Y[16]+Y[59]    X[17]=Y[17]+Y[60]    X[18]=Y[18]+Y[61]   X[19]=Y[19]+Y[62]
// X[20]=Y[20]+Y[63]   X[21]=R[0]+Y[21]     X[22]=R[1]+Y[22]     X[23]=R[2]+Y[23]    X[24]=R[3]+Y[24]
// X[25]=R[4]+Y[25]    X[26]=R[5]+Y[26]     X[27]=R[6]+Y[27]     X[28]=R[7]+Y[28]    X[29]=R[8]+Y[29]
// X[30]=R[9]+Y[30]    X[31]=R[10]+Y[31]    X[32]=R[11]+Y[32]    X[33]=R[12]+Y[33]   X[34]=R[13]+Y[34]
// X[35]=R[14]+Y[35]   X[36]=R[15]+Y[36]    X[37]=R[16]+Y[37]    X[38]=R[17]+Y[38]   X[39]=R[18]+Y[39]
// X[40]=R[19]+Y[40]   X[41]=R[20]+Y[41]    X[42]=R[21]+Y[42]    X[43]=R[22]+Y[43]   X[44]=R[23]+Y[44]
// X[45]=R[24]+Y[45]   X[46]=R[25]+Y[46]    X[47]=R[26]+Y[47]    X[48]=R[27]+Y[48]   X[49]=R[28]+Y[49]
// X[50]=R[29]+Y[50]   X[51]=R[30]+Y[51]    X[52]=R[31]+Y[52]    X[53]=R[32]+Y[53]   X[54]=R[33]+Y[54]
// X[55]=R[34]+Y[55]   X[56]=R[35]+Y[56]    X[57]=R[36]+Y[57]    X[58]=R[37]+Y[58]   X[59]=R[38]+Y[59]
// X[60]=R[39]+Y[60]   X[61]=R[40]+Y[61]    X[62]=R[41]+Y[62]    X[63]=R[42]+Y[63]


// X43+1 descram procedure
  assign X43_Y[63:0]  =DBIN_DATA[63:0];

always @( posedge GLB_RESET or posedge CLOCK155M ) begin
   if ( GLB_RESET==1'b1 )
      X43_REMAINDER[42:0]                <= 43'd0;
   else begin
      if ( DBIN_DATA_EN==1'b1 )
         X43_REMAINDER[42:0]             <=DBIN_DATA[42:0];
   end
end

always @( posedge GLB_RESET or posedge CLOCK155M ) begin
   if ( GLB_RESET==1'b1 )
      X43_DSCRM_DATA[63:0]               <= 64'd0;
   else begin
      if ( DBIN_DATA_EN==1'b1 ) begin
         X43_DSCRM_DATA[0]      <=X43_Y[0] ^ X43_Y[43];
         X43_DSCRM_DATA[1]      <=X43_Y[1] ^ X43_Y[44];
         X43_DSCRM_DATA[2]      <=X43_Y[2] ^ X43_Y[45];
         X43_DSCRM_DATA[3]      <=X43_Y[3] ^ X43_Y[46];
         X43_DSCRM_DATA[4]      <=X43_Y[4] ^ X43_Y[47];
         X43_DSCRM_DATA[5]      <=X43_Y[5] ^ X43_Y[48];
         X43_DSCRM_DATA[6]      <=X43_Y[6] ^ X43_Y[49];
         X43_DSCRM_DATA[7]      <=X43_Y[7] ^ X43_Y[50];
         X43_DSCRM_DATA[8]      <=X43_Y[8] ^ X43_Y[51];
         X43_DSCRM_DATA[9]      <=X43_Y[9] ^ X43_Y[52];
         X43_DSCRM_DATA[10]     <=X43_Y[10] ^ X43_Y[53];
         X43_DSCRM_DATA[11]     <=X43_Y[11] ^ X43_Y[54];
         X43_DSCRM_DATA[12]     <=X43_Y[12] ^ X43_Y[55];
         X43_DSCRM_DATA[13]     <=X43_Y[13] ^ X43_Y[56];
         X43_DSCRM_DATA[14]     <=X43_Y[14] ^ X43_Y[57];
         X43_DSCRM_DATA[15]     <=X43_Y[15] ^ X43_Y[58];
         X43_DSCRM_DATA[16]     <=X43_Y[16] ^ X43_Y[59];
         X43_DSCRM_DATA[17]     <=X43_Y[17] ^ X43_Y[60];
         X43_DSCRM_DATA[18]     <=X43_Y[18] ^ X43_Y[61];
         X43_DSCRM_DATA[19]     <=X43_Y[19] ^ X43_Y[62];
         X43_DSCRM_DATA[20]     <=X43_Y[20] ^ X43_Y[63];
         X43_DSCRM_DATA[21]     <=X43_REMAINDER[0] ^ X43_Y[21];
         X43_DSCRM_DATA[22]     <=X43_REMAINDER[1] ^ X43_Y[22];
         X43_DSCRM_DATA[23]     <=X43_REMAINDER[2] ^ X43_Y[23];
         X43_DSCRM_DATA[24]     <=X43_REMAINDER[3] ^ X43_Y[24];
         X43_DSCRM_DATA[25]     <=X43_REMAINDER[4] ^ X43_Y[25];
         X43_DSCRM_DATA[26]     <=X43_REMAINDER[5] ^ X43_Y[26];
         X43_DSCRM_DATA[27]     <=X43_REMAINDER[6] ^ X43_Y[27];
         X43_DSCRM_DATA[28]     <=X43_REMAINDER[7] ^ X43_Y[28];
         X43_DSCRM_DATA[29]     <=X43_REMAINDER[8] ^ X43_Y[29];
         X43_DSCRM_DATA[30]     <=X43_REMAINDER[9] ^ X43_Y[30];
         X43_DSCRM_DATA[31]     <=X43_REMAINDER[10] ^ X43_Y[31];
         X43_DSCRM_DATA[32]     <=X43_REMAINDER[11] ^ X43_Y[32];
         X43_DSCRM_DATA[33]     <=X43_REMAINDER[12] ^ X43_Y[33];
         X43_DSCRM_DATA[34]     <=X43_REMAINDER[13] ^ X43_Y[34];
         X43_DSCRM_DATA[35]     <=X43_REMAINDER[14] ^ X43_Y[35];
         X43_DSCRM_DATA[36]     <=X43_REMAINDER[15] ^ X43_Y[36];
         X43_DSCRM_DATA[37]     <=X43_REMAINDER[16] ^ X43_Y[37];
         X43_DSCRM_DATA[38]     <=X43_REMAINDER[17] ^ X43_Y[38];
         X43_DSCRM_DATA[39]     <=X43_REMAINDER[18] ^ X43_Y[39];
         X43_DSCRM_DATA[40]     <=X43_REMAINDER[19] ^ X43_Y[40];
         X43_DSCRM_DATA[41]     <=X43_REMAINDER[20] ^ X43_Y[41];
         X43_DSCRM_DATA[42]     <=X43_REMAINDER[21] ^ X43_Y[42];
         X43_DSCRM_DATA[43]     <=X43_REMAINDER[22] ^ X43_Y[43];
         X43_DSCRM_DATA[44]     <=X43_REMAINDER[23] ^ X43_Y[44];
         X43_DSCRM_DATA[45]     <=X43_REMAINDER[24] ^ X43_Y[45];
         X43_DSCRM_DATA[46]     <=X43_REMAINDER[25] ^ X43_Y[46];
         X43_DSCRM_DATA[47]     <=X43_REMAINDER[26] ^ X43_Y[47];
         X43_DSCRM_DATA[48]     <=X43_REMAINDER[27] ^ X43_Y[48];
         X43_DSCRM_DATA[49]     <=X43_REMAINDER[28] ^ X43_Y[49];
         X43_DSCRM_DATA[50]     <=X43_REMAINDER[29] ^ X43_Y[50];
         X43_DSCRM_DATA[51]     <=X43_REMAINDER[30] ^ X43_Y[51];
         X43_DSCRM_DATA[52]     <=X43_REMAINDER[31] ^ X43_Y[52];
         X43_DSCRM_DATA[53]     <=X43_REMAINDER[32] ^ X43_Y[53];
         X43_DSCRM_DATA[54]     <=X43_REMAINDER[33] ^ X43_Y[54];
         X43_DSCRM_DATA[55]     <=X43_REMAINDER[34] ^ X43_Y[55];
         X43_DSCRM_DATA[56]     <=X43_REMAINDER[35] ^ X43_Y[56];
         X43_DSCRM_DATA[57]     <=X43_REMAINDER[36] ^ X43_Y[57];
         X43_DSCRM_DATA[58]     <=X43_REMAINDER[37] ^ X43_Y[58];
         X43_DSCRM_DATA[59]     <=X43_REMAINDER[38] ^ X43_Y[59];
         X43_DSCRM_DATA[60]     <=X43_REMAINDER[39] ^ X43_Y[60];
         X43_DSCRM_DATA[61]     <=X43_REMAINDER[40] ^ X43_Y[61];
         X43_DSCRM_DATA[62]     <=X43_REMAINDER[41] ^ X43_Y[62];
         X43_DSCRM_DATA[63]     <=X43_REMAINDER[42] ^ X43_Y[63];
      end
   end
end
always @( posedge GLB_RESET or posedge CLOCK155M ) begin
   if ( GLB_RESET==1'b1 )
      X43_DSCRM_DATA_EN              <= 1'b0;
   else
      X43_DSCRM_DATA_EN              <= DBIN_DATA_EN;
end



// no scram precedure
always @( posedge GLB_RESET or posedge CLOCK155M ) begin
   if ( GLB_RESET==1'b1 ) begin
      NSCRM_DATA[63:0]               <= 64'd0;
      NSCRM_DATA_EN                  <= 1'b0;
   end
   else begin
      NSCRM_DATA[63:0]               <= DBIN_DATA[63:0];
      NSCRM_DATA_EN                  <= DBIN_DATA_EN;
   end
end


// outut select with 
// clock in CPU control signal
always @( posedge GLB_RESET or posedge CLOCK155M ) begin
   if ( GLB_RESET==1'b1 ) begin
      DSEL_SCRAM_EN                  <= 1'b0;
      DSEL_SCRAM_MODE                <= 1'b0;
   end
   else begin
      DSEL_SCRAM_EN                  <= CFG_POS_PAYLOAD_SCRAM_EN;
      DSEL_SCRAM_MODE                <= CFG_POS_PAYLOAD_SCRAM_MODE;
   end
end


always @( posedge GLB_RESET or posedge CLOCK155M ) begin
   if ( GLB_RESET==1'b1 ) 
   begin
      DBOUT_DATA[63:0]               <= 64'd0;
      DBOUT_DATA_EN                  <= 1'b0;
   end
   else 
   begin
//		if (STOP_RX == 1'b1)
//		begin
//		    DBOUT_DATA[63:0]            <= 64'hffff_ffff_ffff_ffff;
//            DBOUT_DATA_EN               <= 1'b0;
//		end
//	    else if ( DSEL_SCRAM_EN==1'b0 ) 
        if ( DSEL_SCRAM_EN==1'b0 ) 
		begin                         // no scram mode
			DBOUT_DATA[63:0]            <= NSCRM_DATA[63:0];
			DBOUT_DATA_EN               <= NSCRM_DATA_EN;
		end
		else if ( DSEL_SCRAM_MODE==1'b0 ) 
		begin                    // X43+1 scram mode
			DBOUT_DATA[63:0]         <= X43_DSCRM_DATA[63:0];
			DBOUT_DATA_EN            <= X43_DSCRM_DATA_EN;
		end
		else 
		begin                                            // others mode, default as no scram mode
			DBOUT_DATA[63:0]         <= NSCRM_DATA[63:0];
			DBOUT_DATA_EN            <= NSCRM_DATA_EN;
		end
   end
end

endmodule 
